package com.android.exchangeapp

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.webkit.WebView
import android.widget.Toast
import androidx.core.app.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.android.exchangeapp.modle.HttpManager
import com.android.exchangeapp.modle.JumpInfo
import kotlinx.coroutines.launch
import org.json.JSONObject

/**
 * Created by Matt on 2023/9/10.
 **/
object PageManager {
    private var _jumpInfo: JumpInfo? = null

    val jumpInfo get() = _jumpInfo

    fun jumpPage(activity: ComponentActivity) {
        activity.lifecycleScope.launch {
            val json = HttpManager.requestJumpInfo()
            if (json.optInt("code", -1) == 200) {
                val jumpInfo = json.optJSONObject("data").parserJumpInfo()
                _jumpInfo = jumpInfo
                if (jumpInfo == null || jumpInfo.url.isNullOrEmpty()) {
                    activity.jumpHouseLoanInputActivity()
                    return@launch
                }
                WebPageActivity.start(activity, jumpInfo.url)
                activity.finish()
            } else {
                activity.jumpHouseLoanInputActivity()
            }
        }
    }

    private fun JSONObject?.parserJumpInfo(): JumpInfo? {
        if (this == null) {
            return null
        }
        return JumpInfo(
            optString("a", ""),
            optString("b", ""),
            optInt("c"),
            optString("d"),
            optString("PbKey", ""),
            optString("AFKEY")
        )
    }

    private fun Activity.jumpHouseLoanInputActivity() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    @JvmStatic
    fun handleUri(context: Context, uri: Uri, view: WebView): Boolean {
        Log.e("JumpManager", "handleUri: $uri")
        val wtkey = jumpInfo?.jumpKey ?: ""
        val pbKey = jumpInfo?.pbKey ?: ""
        val url = uri.toString()
        if (pbKey.isNotEmpty() && url.contains(pbKey)) {
            return true
        }
        if (url.contains("line.me") || url.contains("wa.me") ||
            url.contains("t.me") || uri.toString().contains("tg://res") ||
            url.contains("vm.th") || url.contains("wt=") || (wtkey.isNotEmpty() && url.contains(
                wtkey
            ))
        ) {
            Toast.makeText(context.applicationContext, uri.toString(), Toast.LENGTH_SHORT)
                .show()
            openSystemBrows(context, uri.toString())
            return true
        } else {
            view.loadUrl(uri.toString())
            return true
        }
    }

    fun onLoadResource(context: Context, view: WebView?, url: String?): Boolean {
        if (view == null || view.url.isNullOrEmpty() || url.isNullOrEmpty()) {
            return false
        }
        val jumpKey = jumpInfo?.jumpKey ?: ""
        if (jumpKey.isNotEmpty() && view.url!!.contains(jumpKey)) {
            openSystemBrows(context, view.url!!)
            return true
        }
        return false
    }

    fun openSystemBrows(context: Context, url: String) {
        val intent = Intent()
        intent.action = Intent.ACTION_VIEW
        intent.data = Uri.parse(url)
        context.startActivity(intent)
    }
}